package com.pai4j.api.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.enums.ResponseCodeEnum;
import com.pai4j.remote.aigc.ContextServiceClient;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/chat/session")
public class ContextApiController {

    @Resource
    private ContextServiceClient contextServiceClient;

    @PostMapping("/create")
    @SentinelResource(value = "api_session_create", blockHandler = "block")
    public PAIResponseBean<String> create(@RequestParam String userId,
                                          @RequestParam String botId,
                                          @RequestParam(required = false) String sessionId) {
        return contextServiceClient.createSession(userId, botId, sessionId);
    }

    @PostMapping("/rename")
    @SentinelResource(value = "api_session_rename", blockHandler = "block")
    public PAIResponseBean<Boolean> rename(@RequestParam String sessionId,
                                           @RequestParam String title) {
        return contextServiceClient.rename(sessionId, title);
    }

    @PostMapping("/close")
    @SentinelResource(value = "api_session_close", blockHandler = "block")
    public PAIResponseBean<Boolean> close(@RequestParam String sessionId) {
        return contextServiceClient.close(sessionId);
    }

    @GetMapping("/list")
    @SentinelResource(value = "api_session_list", blockHandler = "block")
    public PAIResponseBean<List<Map<String, Object>>> list(@RequestParam String userId,
                                                           @RequestParam(required = false) String botId,
                                                           @RequestParam(required = false) Integer page,
                                                           @RequestParam(required = false) Integer size,
                                                           @RequestParam(required = false) String sortField,
                                                           @RequestParam(required = false) String order) {
        return contextServiceClient.list(userId, botId, page, size, sortField, order);
    }

    @GetMapping("/messages")
    @SentinelResource(value = "api_session_messages", blockHandler = "block")
    public PAIResponseBean<List<Map<String, Object>>> messages(@RequestParam String sessionId,
                                                               @RequestParam(defaultValue = "20") Integer limit) {
        return contextServiceClient.messages(sessionId, limit);
    }

    @GetMapping("/messages-json")
    @SentinelResource(value = "api_session_messages_json", blockHandler = "block")
    public PAIResponseBean<String> messagesJson(@RequestParam String sessionId,
                                                @RequestParam(defaultValue = "20") Integer limit) {
        return contextServiceClient.messagesJson(sessionId, limit);
    }

    @PostMapping("/summarize")
    @SentinelResource(value = "api_session_summarize", blockHandler = "block")
    public PAIResponseBean<Boolean> summarize(@RequestParam String sessionId) {
        return contextServiceClient.summarize(sessionId);
    }

    @GetMapping("/export")
    @SentinelResource(value = "api_session_export", blockHandler = "block")
    public PAIResponseBean<List<Map<String, Object>>> export(@RequestParam String sessionId) {
        return contextServiceClient.export(sessionId);
    }

    @PostMapping("/message/delete")
    @SentinelResource(value = "api_message_delete", blockHandler = "block")
    public PAIResponseBean<Boolean> deleteMessage(@RequestParam Long id) {
        return contextServiceClient.deleteMessage(id);
    }

    @PostMapping("/clear")
    @SentinelResource(value = "api_session_clear", blockHandler = "block")
    public PAIResponseBean<Boolean> clear(@RequestParam String sessionId) {
        return contextServiceClient.clear(sessionId);
    }

    // Sentinel block handler (method signature must match + BlockException at end)
    public PAIResponseBean block(String p1, String p2, String p3, String p4, String p5, String p6, BlockException ex) {
        return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(),"Too many requests, please retry later.");
    }

    public PAIResponseBean block(String p1, String p2, String p3, String p4, String p5, BlockException ex) {
        return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(),"Too many requests, please retry later.");
    }

    public PAIResponseBean block(String p1, String p2, BlockException ex) {
        return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(),"Too many requests, please retry later.");
    }

    public PAIResponseBean block(String p1, BlockException ex) {
        return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(),"Too many requests, please retry later.");
    }
}
